import { safeBill, dangerBill1, dangerBill2 } from '../../../../services/fundposition_service';

export default {
  namespace: 'billSituationRule',
  state: {
    safeBilldata: {
      code: '',
      message: '',
      list: [],
    },

    dangerBill1data: {
      code: '',
      message: '',
      list: [],
    },

    dangerBill2data: {
      code: '',
      message: '',
      list: [],
    },
  },

  effects: {
    // 距离执行价比例<=5%的票据
    *DangerBill2({ payload, callback }, { call, put }) {
      const response = yield call(dangerBill2, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const res = response;
      if (callback) callback(res);
      yield put({
        type: 'saveDangerBill2data',
        payload: res,
      });
    },
    // 跌破执行价的票据
    *DangerBill1({ payload, callback }, { call, put }) {
      const response = yield call(dangerBill1, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const res = response;
      if (callback) callback(res);
      yield put({
        type: 'saveDangerBill1data',
        payload: res,
      });
    },

    // 即将到期（7月&8月）的安全票据
    *SafeBill({ payload, callback }, { call, put }) {
      const response = yield call(safeBill, payload);
      if (typeof response === 'undefined') {
        return;
      }
      const res = response;
      if (callback) callback(res);
      yield put({
        type: 'saveSafeBilldata',
        payload: res,
      });
    },
  },

  reducers: {
    saveSafeBilldata(state, action) {
      return {
        ...state,
        safeBilldata: action.payload,
      };
    },

    saveDangerBill1data(state, action) {
      return {
        ...state,
        dangerBill1data: action.payload,
      };
    },

    saveDangerBill2data(state, action) {
      return {
        ...state,
        dangerBill2data: action.payload,
      };
    },
  },
};
